$(function(){
    var Alert1 = new Alert();
    var Alert2 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $leftArrow = $(".leftArrow");
    var $rightArrow = $(".rightArrow");
    var $bannerBox = $(".banner_box");
    var $tableBox = $(".tableBox");
    var $searchBtn = $(".searchBtn");
    var $addStu = $(".addStu");
    var $mb = $(".mb");
    var addStuModel = "<div class=\"popup\">"
        +"<div class=\"popup_box\">"
        +"<div class=\"popup_contenthead\">添加学生<img class=\"popup_img\" src=\"pc/images/ico_alertDel.png\"></div>"
        +"<div class=\"popup_content\" style=\"padding: 20px 125px 5px;\">"
        +"<div>&emsp;&emsp;院系:&ensp;<input autocomplete=\"off\" type=\"text\" style='background: #eeeeee' disabled='disabled' value='"+ result.paramData.depart +"' class=\"publicText\" maxlength=\"20\" placeholder=\"请输入院系\" id=\"depart\" /></div>"
        +"<div>&emsp;&emsp;专业:&ensp;<input autocomplete=\"off\" type=\"text\" style='background: #eeeeee' disabled='disabled' value='"+ result.paramData.profess +"' class=\"publicText\" maxlength=\"20\" placeholder=\"请输入专业\" id=\"profess\" /></div>"
        +"<div>&emsp;&emsp;学号:&ensp;<input autocomplete=\"off\" type=\"text\" class=\"publicText\" maxlength=\"15\" placeholder=\"请输入学号\" id=\"stuorTea\" /></div>"
        +"<div>&emsp;&emsp;姓名:&ensp;<input autocomplete=\"off\" type=\"text\" class=\"publicText\" maxlength=\"20\"  placeholder=\"请输入姓名\" id=\"name\"/></div>"
        +"<div>&emsp;&emsp;性别:&ensp;<span data-sex=\"m\" class=\"male radioCheck\">男</span><span data-sex=\"f\"  class=\"female radio\">女</span></div>"
        +"<div>联系方式:&ensp;<input type=\"text\" class=\"publicText\"  placeholder=\"请输入联系方式\" id=\"telNum\" /></div>"
        +"<div style='font-size: 14px'><span style='color: #C80211'>提示 :</span>&ensp;学生初始密码默认000000</div>"
        +"</div>"
        +"<div class=\"popup_btn\">"
        +"<a class=\"popup_confirm exportBtn hover-btn\">确定</a>"
        +"<a class=\"popup_close exportCancle hover-btn\">取消</a>"
        +"</div>"
        +"<div class=\"clear\"></div>"
        +"</div>"
        +"</div>";
    var th = "<tr>" +
        "<th class=\"col-lg-1\"><input type=\"checkbox\" name=\"controlAll\" id=\"controlAll\" class=\"allCheck\"></th>" +
        "<th class=\"col-lg-1\">学生姓名</th>" +
        "<th class=\"col-lg-1\">学号</th>" +
        "<th class=\"col-lg-1\">性别</th>" +
        "<th class=\"col-lg-2\">院系</th>" +
        "<th class=\"col-lg-2\">专业</th>" +
        "<th class=\"col-lg-2\">班级</th>" +
        "<th class=\"col-lg-2\">操作</th>" +
        "</tr>";

    function tabelModel(opt) {
        var sex = opt.userSex;
        if(sex == "m"){
            sex = "男"
        }
        if(sex == "f"){
            sex = "女"
        }
        return "<tr data-num=\"0\">" +
            "<td class=\"col-lg-1\"><input data-stuorteaid=" + opt.stuorteaId + " value=\"0\" name=\"selecteds\" type=\"checkbox\" class=\"oneCheck\" /></td>" +
            "<td class=\"col-lg-1\" title=" + opt.userName + ">" + opt.userName + "</td>" +
            "<td class=\"col-lg-1\" title=" + opt.stuorteaId + ">" + opt.stuorteaId + "</td>" +
            "<td class=\"col-lg-1\">" + sex + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.departmentName + ">" + opt.departmentName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.professionName + ">" + opt.professionName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.className + ">" + opt.className + "</td>" +
            "<td class=\"col-lg-2\">" +
            "<a href=\"javascript:;\" data-stuorteaid=" + opt.stuorteaId + "  class=\"editStuBtn\">&emsp;&emsp;修改</a>" +
            "<a href=\"javascript:;\" data-stuorteaid=" + opt.stuorteaId + "  class=\"detailBtn\">初始化密码</a>" +
            "</td>" +
            "</tr>";
    }
    var departMana = {
        init:function(){
            this.bindEvent();
            maxLengthLimit.init("publicText");
        },
        bindEvent:function(){
            var self = this;
            self.allCheckEvent();
            $(".delStu").off("click").on("click",function(){
                self.delListEvent();
            });
            var json = {
                "classId":result.paramData.classId,
                "pageNow":1,
                "pageSize":10,
                "selecetRoleId":1,
                "roleId":1
            };
            //获取数据
            self.getDataEvent(json);
            //搜索
            $searchBtn.off("click").on("click", function() {
                var userName = $("#departName").val();
                var jsonStr = {
                    "pageNow": 1,
                    "classId":result.paramData.classId,
                    "pageSize": 10,
                    "selecetRoleId":1,
                    "roleId":1
                };
                var flag = self.verifications(userName);
                if(flag == true){
                    if(userName != ""){
                        jsonStr.userName = userName;
                    }
                    self.getDataEvent(jsonStr);
                }
            });
//			添加学生
            $addStu.off("click").on("click", function() {
                self.addStuEvent();
                maxLengthLimit.init("publicText");
            });
            $(".looktg").off("click").click(function () {
                    var json = JSON.stringify({
                        "pageName": $(this).attr("data-url"),
                        "paramData": {
                            "some": result.paramData.some
                        }
                    });
                    window.location.href = "skipPage?jsonStr=" + json;
            });
        },
        editStuEvent:function(){
//			修改学生信息
            var self = this;
            $(".editStuBtn").off("click").click(function(){

                self.popupEvent();
                $(".popup_contenthead").html("修改学生<img class=\"popup_img\" src=\"pc/images/ico_alertDel.png\">");
                $(".popup_img").off("click").on("click", function() {
                    $(".popup").remove();
                    $mb.hide();
                });
                var jsonStr = JSON.stringify({
                    "stuorteaId":$(this).attr("data-stuorteaid")
                });
                maxLengthLimit.init("publicText");
                $.ajax({
                    type:"post",
                    url:"getMemberMsg",
                    async:true,
                    data:{
                        "jsonStr":jsonStr
                    },
                    success:function(msg){
                        msg = JSON.parse(msg);
                        $("#stuorTea").val(msg.stuorteaId).attr("disabled","disabled").css({"background":"#eeeeee"});
                        $("#name").val(msg.userName);
                        $("#telNum").val(msg.telephoneNum);
                        if(msg.userSex == "m"){
                            $(".male").removeClass("radio").addClass("radioCheck");
                            $(".female").removeClass("radioCheck").addClass("radio");
                        }else{
                            $(".female").removeClass("radio").addClass("radioCheck");
                            $(".male").removeClass("radioCheck").addClass("radio");
                        }
                    }
                });
                $(".exportBtn").off("click").on("click", function() {
                    var flag = self.addStuVer();
                    if(flag == true){
                        self.editStuSubmit();
                    }
                });
            });

        },
        editStuSubmit:function(){
            //			编辑学生提交
            var self = this;
            var jsonStr = JSON.stringify({
                "userContect":"",
                "stuorteaId":$("#stuorTea").val(),
                "telephoneNum":$("#telNum").val(),
                "userName":$("#name").val(),
                "userSex":$(".radioCheck").attr("data-sex")
            });
            $.ajax({
                type:"post",
                url:"saveMemberMsg",
                async:true,
                data:{
                    "jsonStr":jsonStr
                },
                success:function(msg){
                    msg = JSON.parse(msg);
                    if(msg.result == "success"){
                        Alert1.content = "修改学生成功";
                        Alert1.success = function () {
                            $(".popup").remove();
                            $mb.hide();
                            var json = {
                                "classId":result.paramData.classId,
                                "pageNow":1,
                                "pageSize":10,
                                "selecetRoleId":1,
                                "roleId":1
                            };
                            //获取数据
                            $("#departName").val("");
                            self.getDataEvent(json);
                        };
                        Alert1.init();
                        Alert1.show();

                    }else{
                        Alert2.content = msg.failMsg;
                        Alert2.success = function () {
                            $(".popup").show();
                            $mb.show();
                        };
                        Alert2.init();
                        Alert2.show();
                    }
                },
                error:function () {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;
                }
            });
        },
        popupEvent:function(){
//			弹出盒中相关事件
            //取消和X事件
            if($(".popup").length!=0){
                $(".popup").remove();
            }else{
                $mb.show();
                $("body").append(addStuModel);

            }
            $(".popup_img").off("click").on("click", function() {
                $(".popup").remove();
                $mb.hide();
            });
            $(".exportCancle").off("click").on("click", function() {
                $(".popup").remove();
                $mb.hide();
            });
            //性别选择
            $(".male").off("click").on("click", function() {
                $(this).removeClass("radio").addClass("radioCheck");
                $(".female").removeClass("radioCheck").addClass("radio");
            });
            $(".female").off("click").on("click", function() {
                $(this).removeClass("radio").addClass("radioCheck");
                $(".male").removeClass("radioCheck").addClass("radio");
            });
        },
        addStuEvent:function(){
            var self = this;
            self.popupEvent();
            $(".exportBtn").off("click").on("click", function() {
                var flag = self.addStuVer();
                if(flag){
                    self.addStuSubmit();
                }
            });
        },
        addStuSubmit:function(){
//			添加学生
            var self = this;
            var jsonStr = JSON.stringify({
                "age":0,
                "classId":result.paramData.classId,
                "headIcon":"",
                "stuorteaId":$("#stuorTea").val(),
                "telephoneNum":$("#telNum").val(),
                "userName":$("#name").val(),
                "userSex":$(".radioCheck").attr("data-sex")
            });
            $.ajax({
                type:"post",
                url:"addStu",
                async:true,
                data:{
                    "jsonStr":jsonStr
                },
                success:function(msg){
                    msg = JSON.parse(msg);
                    if(msg.result == "success"){
                        Alert1.content = "添加学生成功";
                        Alert1.success  = function () {
                            window.location.reload(true);
                        };
                        Alert1.init();
                        Alert1.show();
                    }else{
                        Alert2.content = msg.failMsg;
                        Alert2.success = function () {
                            $(".popup").show();
                            $mb.show();
                        };
                        Alert2.init();
                        Alert2.show();
                    }
                },
                error:function () {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;
                }
            });
        },
        addStuVer:function(){
//			添加学生验证
            var stuorTeaVal = $("#stuorTea").val();
            if(stuorTeaVal == ""){
                Alert1.content = "请输入学生学号";
                Alert1.init();
                Alert1.show();
                return false;
            }
            var flag = verification.spaceCode(stuorTeaVal);
            var flags = verification.specialCode(stuorTeaVal);
            var flagst = verification.stuorTeaIdcode(stuorTeaVal);
            if(flag != true) {
                Alert1.content = flag;
                $("#stuorTea").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            if(flags != true) {
                Alert1.content = flags;
                $("#stuorTea").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            if(flagst != true) {
                Alert1.content = flagst;
                $("#stuorTea").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            var nameVal = $("#name").val();
            if(nameVal == ""){
                Alert1.content = "请输入学生姓名";
                Alert1.init();
                Alert1.show();
                return false;
            }
            var flagm = verification.spaceCode(nameVal);
            var flagsm = verification.specialCode(nameVal);
            if(flagm != true) {
                Alert1.content = flagm;
                $("#name").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            if(flagsm != true) {
                Alert1.content = flagsm;
                $("#name").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            var telNumVal = $("#telNum").val();
            // if(telNumVal!=""){
            //     var fl = verification.telphoneCode(telNumVal);
            //     if(fl != true) {
            //         Alert1.content = fl;
            //         $("#telNum").val("");
            //         Alert1.init();
            //         Alert1.show();
            //         return false;
            //     }
            // }

            return true;

        },
        verifications:function(classNameVal){
            var flag = verification.spaceCode(classNameVal);
            var flags = verification.specialCode(classNameVal);
            if(flag != true) {
                Alert1.content = flag;
                $("#departName").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            if(flags != true) {
                Alert1.content = flags;
                $("#departName").val("");
                Alert1.init();
                Alert1.show();
                return false;
            }
            return true;

        },
        getDataEvent:function(jsons){
            loading1.show();
            var self = this;
            var json = JSON.stringify(jsons);
            $.ajax({
                type: "post",
                url: "getDetailClass",
                data: {
                    "jsonStr": json
                },
                async: true,
                success: function(msg) {
                    loading1.hide();
                    msg = JSON.parse(msg);
                    // $(".title").html(msg.className+".<span>课程信息</span>");
                    $(".title").html(result.paramData.className+".<span>课程信息</span>");
                    if(msg.result == "success"){
                        if(msg.courseList.length != 0) {
                            //						课程列表
                            $bannerBox.empty();
                            if(msg.courseList.length <= 5){
                                $leftArrow.hide();
                                $rightArrow.hide();
                            }
                            $.map(msg.courseList, function(opt) {
                                if(opt.classEndMark == 0){
                                    $bannerBox.append("<li style=\"background:"+ opt.courseColer +";\"><div title="+ opt.courseName +"><i class=\"iconfont\">"+ opt.courseTitle +"</i>"+ opt.courseName +"</div><div title=\"任课教师:"+ opt.userName +"\">任课教师:"+ opt.userName +"</div></li>");
                                }else{
                                    $bannerBox.append("<li style=\"position:relative;background:"+ opt.courseColer +";\"><img style='position: absolute;right: 0;top: 0;' src='pc/images/ico_closeClass.png' alt=''><div title="+ opt.courseName +"><i class=\"iconfont\">"+ opt.courseTitle +"</i>"+ opt.courseName +"</div><div title=\"任课教师:"+ opt.userName +"\">任课教师:"+ opt.userName +"</div></li>");
                                }
                            });
                            self.courseTap();
                        }else {
                            var trImg = "<li style=\"width:100%\"><img style='margin:0 auto;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></li>";
                            $bannerBox.append(trImg)
                        }

                        $tableBox.empty();
                        if(msg.memberList.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.memberList, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $tableBox.append(th + tabel);
                            self.allCheckEvent();
                            self.resetPassword();
                            self.editStuEvent();

                        } else {
                            var trImg = "<tr><td colspan='8'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg);
                        }
                        var sumpage = msg.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "getDetailClass",
                                    data: {
                                        "jsonStr": JSON.stringify(jsonStr)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        $.map(msg.memberList, function(opt) {
                                            tabel += tabelModel(opt);
                                        });
                                        $tableBox.append(th + tabel);
                                        self.allCheckEvent();
                                        self.editStuEvent();
                                        self.resetPassword();
                                    }
                                });
                            }
                        });
                    }else{
                        if(msg.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
                    }

                }
            });
        },
        courseTap:function(){
//			课程切换
            var n = 0;
            $rightArrow.click(function() {
                n++;
                if(n >= Math.ceil($bannerBox.children("li").length / 5)) {
                    n = 0;
                }
                $bannerBox.css("top", (-1 * n * 124) + "px");

            });
            $leftArrow.click(function() {

                n--;
                if(n < 0) {
                    n = Math.ceil($bannerBox.children("li").length / 5) - 1;
                }
                $bannerBox.css("top", (-1 * n * 124) + "px");

            });
        },
        resetPassword:function(){
            var self  = this;
            $(".detailBtn").off("click").click(function(){
                var stuorteaid = $(this).attr("data-stuorteaid");
                confirm1.content = "确定初始化密码吗？";
                confirm1.success = function() {
                    var jsonStr = JSON.stringify({"stuorteaId":stuorteaid});
                    loading1.show();
                    $.ajax({
                        type: "post",
                        url: "resetPwd",
                        async: true,
                        data: {
                            "jsonStr":jsonStr
                        },
                        success: function(msg) {
                            loading1.hide();
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                Alert1.content = "初始化密码成功";
                                Alert1.success = function(){
                                    var json = {
                                        "classId":result.paramData.classId,
                                        "pageNow":1,
                                        "pageSize":10,
                                        "selecetRoleId":1,
                                        "roleId":1
                                    };
                                    $("#departName").val("");
                                    self.getDataEvent(json);
                                };
                                Alert1.init();
                                Alert1.show();
                            }else{
                                Alert1.content = msg.errorMsg;
                                Alert1.init();
                                Alert1.show();
                            }

                        }

                    });
                };
                confirm1.init();
                confirm1.show();
            });

        },
        delListEvent:function(){
//			删除学生
            var self = this;
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要删除的学生";
                Alert1.init();
                Alert1.show();
            }else{
                var list = [];
                for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                    list.push($(".oneCheck[value=1]").eq(i).attr("data-stuorteaid"));
                }
                confirm1.content = "确定删除这<a style='color:#c82011;text-decoration:underline;font-weight:bolder;' href='javascript:;'>"+$(".oneCheck[value=1]").length+"</a>个学生吗"
                confirm1.success = function() {
                    var  jsons={"list":list,"type":3};
                    loading1.show();
                    $.ajax({
                        type: "post",
                        url: "isVm",
                        data: {
                            "jsonStr": JSON.stringify(jsons)
                        },
                        success: function (msg) {
                            loading1.hide();
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                if(msg.isVmtype == 1){
                                    confirm1.content = msg.msg;
                                    confirm1.success = function () {
                                        $(".popups,.masking").show();
                                        $(".outVirBtn").off("click").click(function() {
                                            var jsonStr = JSON.stringify({"list":list,"marks":1});
                                            loading1.show();
                                            $.ajax({
                                                type: "post",
                                                url: "delMembers",
                                                async: true,
                                                data: {
                                                    "jsonStr":jsonStr,
                                                    "memType":0
                                                },
                                                success: function(msg) {
                                                    loading1.hide();
                                                    msg = JSON.parse(msg);
                                                    if(msg.result == "success") {
                                                        $(".popups,.masking").hide();
                                                        Alert1.content = "删除学生成功";
                                                        Alert1.success = function () {
                                                            var json = {
                                                                "classId":result.paramData.classId,
                                                                "pageNow":1,
                                                                "pageSize":10,
                                                                "selecetRoleId":1,
                                                                "roleId":1
                                                            };
                                                            $("#departName").val("");
                                                            self.getDataEvent(json);
                                                        };
                                                        Alert1.init();
                                                        Alert1.show();
                                                    }else{
                                                        if(msg.msg != "" && msg.msg != undefined){
                                                            Alert1.content = msg.msg;
                                                        }else{
                                                            Alert1.content = "删除学生失败";
                                                        }
                                                        Alert1.init();
                                                        Alert1.show();
                                                    }

                                                },
                                                error:function () {
                                                    var json = JSON.stringify({
                                                        "pageName": "error"
                                                    });
                                                    window.location.href = "skipPage?jsonStr=" + json;
                                                }
                                            });
                                        });
                                        $(".delVirBtn").off("click").click(function() {
                                            var jsonStr = JSON.stringify({"list":list,"marks":2});
                                            loading1.show();
                                            $.ajax({
                                                type: "post",
                                                url: "delMembers",
                                                async: true,
                                                data: {
                                                    "jsonStr":jsonStr,
                                                    "memType":0
                                                },
                                                success: function(msg) {
                                                    loading1.hide();
                                                    msg = JSON.parse(msg);
                                                    if(msg.result == "success") {
                                                        $(".popups,.masking").hide();
                                                        Alert1.content = "删除学生成功";
                                                        Alert1.success = function () {
                                                            var json = {
                                                                "classId":result.paramData.classId,
                                                                "pageNow":1,
                                                                "pageSize":10,
                                                                "selecetRoleId":1,
                                                                "roleId":1
                                                            };
                                                            $("#departName").val("");
                                                            self.getDataEvent(json);
                                                        };
                                                        Alert1.init();
                                                        Alert1.show();
                                                    }else{
                                                        if(msg.msg != "" && msg.msg != undefined){
                                                            Alert1.content = msg.msg;
                                                        }else{
                                                            Alert1.content = "删除学生失败";
                                                        }
                                                        Alert1.init();
                                                        Alert1.show();
                                                    }

                                                }
                                            });
                                        });
                                    };
                                    confirm1.init();
                                    confirm1.show();
                                }else{
                                    var jsonStr = JSON.stringify({"list":list,"marks":1});
                                    loading1.show();
                                    $.ajax({
                                        type: "post",
                                        url: "delMembers",
                                        async: true,
                                        data: {
                                            "jsonStr":jsonStr,
                                            "memType":0
                                        },
                                        success: function(msg) {
                                            loading1.hide();
                                            msg = JSON.parse(msg);
                                            if(msg.result == "success") {
                                                Alert1.content = "删除学生成功";
                                                Alert1.success = function () {
                                                    var json = {
                                                        "classId":result.paramData.classId,
                                                        "pageNow":1,
                                                        "pageSize":10,
                                                        "selecetRoleId":1,
                                                        "roleId":1
                                                    };
                                                    $("#departName").val("");
                                                    self.getDataEvent(json);
                                                };
                                                Alert1.init();
                                                Alert1.show();
                                            }else{
                                                if(msg.msg != "" && msg.msg != undefined){
                                                    Alert1.content = msg.msg;
                                                }else{
                                                    Alert1.content = "删除学生失败";
                                                }
                                                Alert1.init();
                                                Alert1.show();
                                            }

                                        },
                                        error:function () {
                                            var json = JSON.stringify({
                                                "pageName": "error"
                                            });
                                            window.location.href = "skipPage?jsonStr=" + json;
                                        }
                                    });
                                }

                            }else{
                                if(msg.msg != "" && msg.msg != undefined){
                                    Alert1.content = msg.msg;
                                }else{
                                    Alert1.content = "删除失败";
                                }
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });

                };
                confirm1.init();
                confirm1.show();

            }
        },
        allCheckEvent:function(){
            //全选
            allCheck.init("controlAll","oneCheck");
        }
    };
    departMana.init();
});
